/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2020年9月16日
 */
package com.massyframework.beanskin.web.filter;

import java.util.Enumeration;
import java.util.Objects;

import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;

import com.massyframework.beanskin.Module;
import com.massyframework.beanskin.service.ServiceRepository;
import com.massyframework.beanskin.web.ModularServletContext;

/**
 * 关联{@link Module}的过滤器配置
 * 
 * @author huangkh
 *
 */
final class ModuleFilterConfig implements FilterConfig {

	private final Module<?> module;
    private final FilterConfig config;
    private final ServletContext servletContext;

    /**
     * 构造方法
     * @param assembly {@link ServiceExportableModule}, 模块
     * @param config {@link }
     */
    public ModuleFilterConfig(Module<?> module, FilterConfig config) {
        this.module = Objects.requireNonNull(module, "\"module\" cannot be null.");
        this.config = Objects.requireNonNull(config, "\"config\" cannot be null.");
        this.servletContext = this.createModularServletContext();
    }

    /* (non-Javadoc)
     * @see javax.servlet.FilterConfig#getFilterName()
     */
    @Override
    public String getFilterName() {
        return this.config.getFilterName();
    }

    /* (non-Javadoc)
     * @see javax.servlet.FilterConfig#getServletContext()
     */
    @Override
    public ServletContext getServletContext() {
        return this.servletContext;
    }

    /* (non-Javadoc)
     * @see javax.servlet.FilterConfig#getInitParameter(java.lang.String)
     */
    @Override
    public String getInitParameter(String name) {
        return this.config.getInitParameter(name);
    }

    /* (non-Javadoc)
     * @see javax.servlet.FilterConfig#getInitParameterNames()
     */
    @Override
    public Enumeration<String> getInitParameterNames() {
        return this.config.getInitParameterNames();
    }

    /**
     * 关联的模块
     * @return {@link ServiceExportableModule}
     */
    public final Module<?> getAssociatedModule() {
        return this.module;
    }

    /**
     * 创建{@link ModularServletContext}
     * @return {@link ModularServletContext}
     */
    protected ServletContext createModularServletContext() {
       ServiceRepository repo = ServiceRepository.retrieveFrom(this.getAssociatedModule());
       return repo.getService(ServletContext.class);
    }

    /* (non-Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return this.getClass().getSimpleName() + " [module=" + module + "]";
    }
}
